﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TimeTrak.BLL
{
    public class Task
    {
        public int ID { get; set; }
        public string Description { get; set; }

        private bool isCompleted;
        public bool IsCompleted
        {
            get { return isCompleted; }
            set { isCompleted = value; }
        }


        private bool isRunning;
        public bool IsRunning
        {
            get
            {
                return isRunning;
            }
            set
            {
                isRunning = value;
            }
        }

        private decimal estimateAmount;
        public decimal EstimateAmount
        {
            get { return estimateAmount; }
            set { estimateAmount = value; }
        }


        public TimeUnitType TaskTimeUnit { get; set; }
        public List<TimeRecord> TimeRecordList { get; set; }
        public TimeRecord CurrentTimeRecord { get; set; }
        public string Category { get; set; }
        public int Priority { get; set; }

        public decimal TotalTime
        {
            get
            {
                decimal total = (decimal)GetTotalTime().TotalHours;
                return total;
            }
        }



        public Task()
        {
            TimeRecordList = new List<TimeRecord>();
        }

        public Task(int id, string descr, bool isCompleted, bool isRunning, decimal estAmount
                    , TimeUnitType taskTimeUnit, List<TimeRecord> recordList, string category, int priority)
        {
            ID = id;
            Description = descr;
            IsCompleted = isCompleted;
            IsRunning = isRunning;
            EstimateAmount = estAmount;
            TaskTimeUnit = taskTimeUnit;
            TimeRecordList = recordList;
            Category = category;
            Priority = priority;
        }


        private TimeSpan GetTotalTime()
        {
            TimeSpan duration = new TimeSpan();
            foreach (TimeRecord rec in TimeRecordList)
            {
                if (rec.EndTime > DateTime.MinValue)
                {
                    duration = duration.Add(rec.GetDuration());
                }
            }
            return duration;
        }




        public void StartTime()
        {
            TimeRecord tr = new TimeRecord();
            tr.StartTime = DateTime.Now;
            tr.IsManualEdit = false;
            TimeRecordList.Add(tr);
            CurrentTimeRecord = tr;
            this.IsRunning = true;
            Console.Out.WriteLine("Starting Time!");
        }

        public void UpdateTime()
        {
            CurrentTimeRecord.EndTime = DateTime.Now;
            Console.Out.WriteLine("Updating Time!");
        }

        public void StopTime()
        {
            //you should be able to call StopTime() with it's not running
            //and not have it explode...
            if (CurrentTimeRecord != null)
            {
                CurrentTimeRecord.EndTime = DateTime.Now;
                CurrentTimeRecord = null;
            }
            this.IsRunning = false;
            Console.Out.WriteLine("Stopping Time!");
        }

        /// <summary>
        /// Allows for adding (or subtracting) amounts of time for the task.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="amount"></param>
        public void ManualAddTime(DateTime editDate, TimeUnitType type, decimal amount)
        {
            //todo
            //make sure that when you add time, it goes into the day you want it to


            TimeRecord tr = new TimeRecord();
            tr.StartTime = editDate;
            DateTime endTime = tr.StartTime;
            switch (type)
            {
                case TimeUnitType.Minute:
                    endTime = endTime.AddMinutes((double)amount);
                    break;
                case TimeUnitType.Hour:
                    endTime = endTime.AddHours((double)amount);
                    break;
                case TimeUnitType.Day:
                    endTime = endTime.AddDays((double)amount);
                    break;
                default:
                    break;
            }
            tr.EndTime = endTime;
            tr.IsManualEdit = true;
            TimeRecordList.Add(tr);

        }


        public void ManualSetTotalTime(TimeUnitType type, decimal totalAmount)
        {
            TimeRecord tr = new TimeRecord();
            tr.StartTime = DateTime.Now;

            TimeSpan currentTotal;
            TimeSpan desiredTotal;

            int seconds = 0;

            switch (type)
            {
                case TimeUnitType.Minute:
                    seconds = (int)totalAmount * 60;
                    break;
                case TimeUnitType.Hour:
                    seconds = (int)totalAmount * 3600;
                    break;
                case TimeUnitType.Day:
                    seconds = (int)totalAmount * 86400;
                    break;
                default:
                    break;
            }

            desiredTotal = new TimeSpan(0, 0, seconds);
            currentTotal = this.GetTotalTime();

            TimeSpan adjustment = desiredTotal.Subtract(currentTotal);


            tr.EndTime = tr.StartTime.Add(adjustment); ;
            tr.IsManualEdit = true;
            TimeRecordList.Add(tr);
        }

    }
}
